package com.demo.dao.impl;

import com.demo.dao.AccountDao;
import com.demo.pojo.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName AccountDaoImpl
 * @Description TODO
 * @Author Windy
 * @Date 2022/9/4 20:02
 * @Version 1.0
 **/
@Repository
public class AccountDaoImpl  implements AccountDao {
    @Autowired
    JdbcTemplate jdbcTemplate;


    @Override
    public List<Account> findAll() {
        System.out.println("dao....");
        return jdbcTemplate.query("select * from account",new BeanPropertyRowMapper<Account>(Account.class));
    }

    @Override
    public Account findOneById(int id) {
        return jdbcTemplate.query("select * from account where  id=?",new BeanPropertyRowMapper<Account>(Account.class),id).get(0);
    }

    @Override
    public int updateACCount(Account account) {
        int update = jdbcTemplate.update("update account set name=?,money=? where id=?", account.getName(),
                account.getMoney(), account.getId());
        return update;
    }

    @Override
    public int deleteAccount(int id) {
        return jdbcTemplate.update("delete from account where id=?",id);
    }

    @Override
    public int insertAccount(Account account) {
        int update = jdbcTemplate.update("insert into account values(?,?,?)", account.getId(), account.getName(),
                account.getMoney());
        return update;
    }


}
